import {
  AbilityType,
  APIType,
  APIVersion,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType
} from '../../../../../models/constants';
import { ModuleTemplateOption, ModuleTemplateOptionBuilder } from '../../../../../models/model-options';
import { ModuleConfigInitFun } from '../../../../../models/module-models';
import { copyTmp, DirTemplateCreator, dirTmp, renderTmp } from '../../../../../models/template-models';
import { entryBuilder, moduleBuilder } from '../../../../../module-builder';


/**
 * Entry
 *
 * @type {ModuleTemplateOption}
 */
const emptyAbilityEntryOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.STAGE)
  .apiVersion(APIVersion.API10)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.EmptyAbility)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const emptyAbilityEntryTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('ets')
        .add(copyTmp('/entry/stage/service/emptyAbility/api10/src/main/ets/entryability'))
        .add(copyTmp('/entry/stage/service/emptyAbility/api10/src/main/ets/pages'))
        .add(copyTmp('/entry/stage/service/emptyAbility/api10/src/main/ets/entryformability'))
        .add(dirTmp('widget')
          .add(copyTmp('/entry/stage/service/emptyAbility/api10/src/main/ets/widget/pages'))
        )
      )
      .add(dirTmp('resources')
        .add(dirTmp('base')
          .add(dirTmp('element')
            .add(copyTmp('/module/stage/service/common/api10/src/main/resources/base/element/color.json'))
            .add(copyTmp('/module/stage/service/common/api10/src/main/resources/base/element/string.json'))
            .add(copyTmp('/module/stage/service/common/api10/src/main/resources/base/element/float.json'))
          )
          .add(copyTmp('/module/stage/service/common/api10/src/main/resources/base/media'))
          .add(copyTmp('/module/stage/service/common/api10/src/main/resources/base/profile'))
        )
        .add(copyTmp('/entry/stage/service/emptyAbility/api10/src/main/resources/en_US'))
        .add(dirTmp('rawfile'))
        .add(copyTmp('/entry/stage/service/emptyAbility/api10/src/main/resources/zh_CN'))
      )
      .add(renderTmp('/entry/stage/service/emptyAbility/api10/src/main/module.json5'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('/entry/stage/service/emptyAbility/api10/src/ohosTest/ets'))
      .add(copyTmp('/entry/stage/service/emptyAbility/api10/src/ohosTest/resources'))
      .add(renderTmp('/entry/stage/service/emptyAbility/api10/src/ohosTest/module.json5'))
    )
    .add(copyTmp('/entry/stage/service/emptyAbility/api10/src/test'))
  )
  .add(renderTmp('/module/stage/service/common/api10/build-profile.json5'))
  .add(copyTmp('/module/stage/service/common/api10/hvigorfile.ts'))
  .add(copyTmp('/module/stage/service/common/api10/obfuscation-rules.txt'))
  .add(renderTmp('/module/stage/service/common/api10/oh-package.json5'));


const initFun: ModuleConfigInitFun = async (name, option, template) => {
  template.node('src').node('main').node('module.json5').data = { moduleName: name };
  template.node('src').node('ohosTest').node('module.json5').data = { moduleName: name };
  template.node('build-profile.json5').data = { apiType: option.apiType, runtimeOS: option.runtimeOS };
  template.node('oh-package.json5').data = { moduleName: name };
};

entryBuilder.add(emptyAbilityEntryOption, emptyAbilityEntryTemplate, initFun);

/**
 * EmptyAbility
 *
 * @type {ModuleTemplateOption}
 */
const emptyAbilityOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.STAGE)
  .apiVersion(APIVersion.API10)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.EmptyAbility)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const emptyAbilityTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('ets')
        .add(copyTmp('/module/stage/service/application/api10/src/main/ets/applicationability'))
        .add(copyTmp('/module/stage/service/application/api10/src/main/ets/applicationformability'))
        .add(copyTmp('/module/stage/service/application/api10/src/main/ets/pages'))
        .add(copyTmp('/module/stage/service/application/api10/src/main/ets/widget'))
      )
      .add(dirTmp('resources')
        .add(dirTmp('base')
          .add(dirTmp('element')
            .add(copyTmp('/module/stage/service/common/api10/src/main/resources/base/element/color.json'))
            .add(copyTmp('/module/stage/service/common/api10/src/main/resources/base/element/string.json'))
            .add(copyTmp('/module/stage/service/common/api10/src/main/resources/base/element/float.json'))
          )
          .add(copyTmp('/module/stage/service/common/api10/src/main/resources/base/media'))
          .add(copyTmp('/module/stage/service/common/api10/src/main/resources/base/profile'))
        )
        .add(copyTmp('/module/stage/service/application/api10/src/main/resources/en_US'))
        .add(dirTmp('rawfile'))
        .add(copyTmp('/module/stage/service/application/api10/src/main/resources/zh_CN'))
      )
      .add(renderTmp('/module/stage/service/application/api10/src/main/module.json5'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('/module/stage/service/application/api10/src/ohosTest/ets'))
      .add(copyTmp('/module/stage/service/application/api10/src/ohosTest/resources'))
      .add(renderTmp('/module/stage/service/common/api10/src/ohosTest/module.json5'))
    )
    .add(copyTmp('/module/stage/service/common/api10/src/test'))
  )
  .add(renderTmp('/module/stage/service/common/api10/build-profile.json5'))
  .add(copyTmp('/module/stage/service/common/api10/hvigorfile.ts'))
  .add(copyTmp('/module/stage/service/common/api10/obfuscation-rules.txt'))
  .add(renderTmp('/module/stage/service/common/api10/oh-package.json5'));

moduleBuilder.add(emptyAbilityOption, emptyAbilityTemplate, initFun);

/**
 * NativeC
 *
 * @type {ModuleTemplateOption}
 */
const nativeAbilityOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.STAGE)
  .apiVersion(APIVersion.API10)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.NativeC)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const nativeAbilityTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('libs')
    .add(dirTmp('armeabi-v7a'))
  )
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('cpp')
        .add(dirTmp('types')
          .add(dirTmp('libapplication')
            .add(copyTmp('/module/stage/service/nativeApplication/api10/src/main/cpp/types/libnativec/index.d.ts'))
            // eslint-disable-next-line max-len
            .add(renderTmp('/module/stage/service/nativeApplication/api10/src/main/cpp/types/libnativec/oh-package.json5'))
          )
        )
        .add(renderTmp('/module/stage/service/nativeApplication/api10/src/main/cpp/CMakeLists.txt'))
        .add(renderTmp('/module/stage/service/nativeApplication/api10/src/main/cpp/hello.cpp'))
      )
      .add(dirTmp('ets')
        .add(copyTmp('/module/stage/service/nativeApplication/api10/src/main/ets/nativecability'))
        .add(copyTmp('/module/stage/service/nativeApplication/api10/src/main/ets/nativecformability'))
        .add(copyTmp('/module/stage/service/nativeApplication/api10/src/main/ets/pages'))
        .add(copyTmp('/module/stage/service/nativeApplication/api10/src/main/ets/widget'))
      )
      .add(dirTmp('resources')
        .add(copyTmp('/module/stage/service/common/api10/src/main/resources/base'))
        .add(copyTmp('/module/stage/service/nativeApplication/api10/src/main/resources/en_US'))
        .add(dirTmp('rawfile'))
        .add(copyTmp('/module/stage/service/nativeApplication/api10/src/main/resources/zh_CN'))
      )
      .add(renderTmp('/module/stage/service/nativeApplication/api10/src/main/module.json5'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('/module/stage/service/common/api10/src/ohosTest/ets'))
      .add(copyTmp('/module/stage/service/common/api10/src/ohosTest/resources'))
      .add(renderTmp('/module/stage/service/common/api10/src/ohosTest/module.json5'))
    )
    .add(copyTmp('/module/stage/service/common/api10/src/test'))
  )
  .add(renderTmp('/module/stage/service/nativeApplication/api10/build-profile.json5'))
  .add(copyTmp('/module/stage/service/nativeApplication/api10/hvigorfile.ts'))
  .add(copyTmp('/module/stage/service/nativeApplication/api10/obfuscation-rules.txt'))
  .add(renderTmp('/module/stage/service/nativeApplication/api10/oh-package.json5'));

const nativeInitFun: ModuleConfigInitFun = async (name, option, template) => {
  // eslint-disable-next-line max-len
  template.node('src').node('main').node('cpp').node('types').node('libnativec').node('oh-package.json5').data = { moduleName: name };
  template.node('src').node('main').node('cpp').node('CMakeLists.txt').data = { moduleName: name };
  template.node('src').node('main').node('cpp').node('hello.cpp').data = { moduleName: name };
  template.node('src').node('main').node('module.json5').data = { moduleName: name };
  template.node('src').node('ohosTest').node('module.json5').data = { moduleName: name };
  template.node('build-profile.json5').data = { apiType: option.apiType };
  template.node('oh-package.json5').data = { moduleName: name };
};

moduleBuilder.add(nativeAbilityOption, nativeAbilityTemplate, nativeInitFun);

/**
 * NativeC(Entry)
 *
 * @type {ModuleTemplateOption}
 */
const nativeAbilityEntryOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.STAGE)
  .apiVersion(APIVersion.API10)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.NativeC)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const nativeAbilityEntryTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('libs')
    .add(dirTmp('armeabi-v7a'))
  )
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('cpp')
        .add(dirTmp('types')
          .add(dirTmp('libentry')
            .add(copyTmp('/module/stage/service/nativeApplication/api10/src/main/cpp/types/libnativec/index.d.ts'))
            // eslint-disable-next-line max-len
            .add(renderTmp('/module/stage/service/nativeApplication/api10/src/main/cpp/types/libnativec/oh-package.json5'))
          )
        )
        .add(renderTmp('/module/stage/service/nativeApplication/api10/src/main/cpp/CMakeLists.txt'))
        .add(renderTmp('/module/stage/service/nativeApplication/api10/src/main/cpp/hello.cpp'))
      )
      .add(copyTmp('/entry/stage/service/nativeApplication/api10/src/main/ets'))
      .add(copyTmp('/entry/stage/service/nativeApplication/api10/src/main/resources'))
      .add(renderTmp('/entry/stage/service/nativeApplication/api10/src/main/module.json5'))
    )
    .add(dirTmp('ohosTest')
      .add(copyTmp('/module/stage/service/common/api10/src/ohosTest/ets'))
      .add(copyTmp('/module/stage/service/common/api10/src/ohosTest/resources'))
      .add(renderTmp('/entry/stage/service/nativeApplication/api10/src/ohosTest/module.json5'))
    )
    .add(copyTmp('/module/stage/service/common/api10/src/test'))
  )
  .add(renderTmp('/module/stage/service/nativeApplication/api10/build-profile.json5'))
  .add(copyTmp('/module/stage/service/nativeApplication/api10/hvigorfile.ts'))
  .add(copyTmp('/module/stage/service/nativeApplication/api10/obfuscation-rules.txt'))
  .add(renderTmp('/entry/stage/service/nativeApplication/api10/oh-package.json5'));

const nativeEntryInitFun: ModuleConfigInitFun = async (name, option, template) => {
  // eslint-disable-next-line max-len
  template.node('src').node('main').node('cpp').node('types').node('libentry').node('oh-package.json5').data = { moduleName: name };
  template.node('src').node('main').node('cpp').node('CMakeLists.txt').data = { moduleName: name };
  template.node('src').node('main').node('cpp').node('hello.cpp').data = { moduleName: name };
  template.node('src').node('main').node('module.json5').data = { moduleName: name };
  template.node('src').node('ohosTest').node('module.json5').data = { moduleName: name };
  template.node('build-profile.json5').data = { apiType: option.apiType };
  template.node('oh-package.json5').data = { moduleName: name };
};

entryBuilder.add(nativeAbilityEntryOption, nativeAbilityEntryTemplate, nativeEntryInitFun);

/**
 * SharedLibrary
 *
 * @type {ModuleTemplateOption}
 */
const shareLibraryOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.STAGE)
  .apiVersion(APIVersion.API10)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.SharedLibrary)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const shareLibraryTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('ets')
        .add(copyTmp('/module/stage/service/sharedLibrary/api10/src/main/ets/pages'))
      )
      .add(dirTmp('resources')
        .add(dirTmp('base')
          .add(copyTmp('/module/stage/service/sharedLibrary/api10/src/main/resources/base/element'))
          .add(copyTmp('/module/stage/service/sharedLibrary/api10/src/main/resources/base/media'))
          .add(copyTmp('/module/stage/service/sharedLibrary/api10/src/main/resources/base/profile'))
        )
        .add(dirTmp('rawfile'))
      )
      .add(renderTmp('/module/stage/service/sharedLibrary/api10/src/main/module.json5'))
    )
    .add(copyTmp('/module/stage/service/common/api10/src/test'))
  )
  .add(renderTmp('/module/stage/service/sharedLibrary/api10/build-profile.json5'))
  .add(copyTmp('/module/stage/service/sharedLibrary/api10/hvigorfile.ts'))
  .add(copyTmp('/module/stage/service/common/api10/obfuscation-rules.txt'))
  .add(renderTmp('/module/stage/service/sharedLibrary/api10/oh-package.json5'));

const libraryInitFun: ModuleConfigInitFun = async (name, option, template) => {
  template.node('src').node('main').node('module.json5').data = { moduleName: name };
  template.node('build-profile.json5').data = { apiType: option.apiType };
  template.node('oh-package.json5').data = { moduleName: name };
};

moduleBuilder.add(shareLibraryOption, shareLibraryTemplate, libraryInitFun);

/**
 * SharedLibraryWithC
 *
 * @type {ModuleTemplateOption}
 */
const shareLibraryWithCOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.STAGE)
  .apiVersion(APIVersion.API10)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.SharedLibraryWithC)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const shareLibraryWithCTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('libs')
    .add(dirTmp('armeabi-v7a'))
  )
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('cpp')
        .add(dirTmp('types')
          .add(dirTmp('liblibrary')
            .add(copyTmp('/module/stage/service/nativeApplication/api10/src/main/cpp/types/libnativec/index.d.ts'))
            // eslint-disable-next-line max-len
            .add(renderTmp('/module/stage/service/nativeApplication/api10/src/main/cpp/types/libnativec/oh-package.json5')))
        )
        .add(renderTmp('/module/stage/service/sharedLibraryWithC/api10/src/main/cpp/CMakeLists.txt'))
        .add(renderTmp('/module/stage/service/nativeApplication/api10/src/main/cpp/hello.cpp'))
      )
      .add(dirTmp('ets')
        .add(copyTmp('/module/stage/service/sharedLibraryWithC/api10/src/main/ets/pages'))
      )
      .add(dirTmp('resources')
        .add(dirTmp('base')
          .add(copyTmp('/module/stage/service/sharedLibrary/api10/src/main/resources/base/element'))
          .add(copyTmp('/module/stage/service/sharedLibrary/api10/src/main/resources/base/media'))
          .add(copyTmp('/module/stage/service/sharedLibrary/api10/src/main/resources/base/profile'))
        )
        .add(dirTmp('rawfile'))
      )
      .add(renderTmp('/module/stage/service/sharedLibrary/api10/src/main/module.json5'))
    )
    .add(copyTmp('/module/stage/service/common/api10/src/test'))
  )
  .add(renderTmp('/module/stage/service/sharedLibrary/api10/build-profile.json5'))
  .add(copyTmp('/module/stage/service/sharedLibrary/api10/hvigorfile.ts'))
  .add(copyTmp('/module/stage/service/common/api10/obfuscation-rules.txt'))
  .add(renderTmp('/module/stage/service/sharedLibrary/api10/oh-package.json5'));

const libraryWithCInitFun: ModuleConfigInitFun = async (name, option, template) => {
  template.node('src').node('main').node('cpp').node('types').node('liblibrary')
    .node('oh-package.json5').data = { moduleName: name };
  template.node('src').node('main').node('cpp').node('CMakeLists.txt').data = { moduleName: name };
  template.node('src').node('main').node('cpp').node('hello.cpp').data = { moduleName: name };
  template.node('src').node('main').node('module.json5').data = { moduleName: name };
  template.node('build-profile.json5').data = { apiType: option.apiType };
  template.node('oh-package.json5').data = { moduleName: name, main: 'Index.ets', license: 'Apache-2.0' };
};
moduleBuilder.add(shareLibraryWithCOption, shareLibraryWithCTemplate, libraryWithCInitFun);


/**
 * StaticLibrary
 *
 * @type {ModuleTemplateOption}
 */
const staticLibraryOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.STAGE)
  .apiVersion(APIVersion.API10)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.StaticLibrary)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const staticLibraryTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(copyTmp('/module/stage/service/staticLibrary/api10/src/main/ets'))
      .add(copyTmp('/module/stage/service/staticLibrary/api10/src/main/resources'))
      .add(renderTmp('/module/stage/service/staticLibrary/api10/src/main/module.json5'))
    )
    .add(copyTmp('/module/stage/service/common/api10/src/test'))
  )
  .add(renderTmp('/module/stage/service/staticLibrary/api10/build-profile.json5'))
  .add(copyTmp('/module/stage/service/common/api10/consumer-rules.txt'))
  .add(copyTmp('/module/stage/service/staticLibrary/api10/hvigorfile.ts'))
  .add(copyTmp('/module/stage/service/staticLibrary/api10/Index.ets'))
  .add(copyTmp('/module/stage/service/common/api10/obfuscation-rules.txt'))
  .add(renderTmp('/module/stage/service/staticLibrary/api10/oh-package.json5'));

moduleBuilder.add(staticLibraryOption, staticLibraryTemplate, libraryInitFun);

/**
 * StaticLibraryWithC
 *
 * @type {ModuleTemplateOption}
 */
const staticLibraryWithCOption: ModuleTemplateOption = ModuleTemplateOptionBuilder.builder()
  .abilityType(AbilityType.AtomicService)
  .apiType(APIType.STAGE)
  .apiVersion(APIVersion.API10)
  .language(LanguageType.ArkTs)
  .moduleTemplateType(ModuleTemplateType.StaticLibraryWithC)
  .runtimeOS(RuntimeOSType.HarmonyOS)
  .build();

const staticLibraryWithCTemplate: DirTemplateCreator = (dirName) => dirTmp(dirName)
  .add(dirTmp('libs')
    .add(dirTmp('armeabi-v7a'))
  )
  .add(dirTmp('src')
    .add(dirTmp('main')
      .add(dirTmp('cpp')
        .add(dirTmp('types')
          .add(dirTmp('liblibrary')
            .add(copyTmp('/module/stage/service/nativeApplication/api10/src/main/cpp/types/libnativec/index.d.ts'))
            // eslint-disable-next-line max-len
            .add(renderTmp('/module/stage/service/nativeApplication/api10/src/main/cpp/types/libnativec/oh-package.json5')))
        )
        .add(renderTmp('/module/stage/service/sharedLibraryWithC/api10/src/main/cpp/CMakeLists.txt'))
        .add(renderTmp('/module/stage/service/nativeApplication/api10/src/main/cpp/hello.cpp'))
      )
      .add(copyTmp('/module/stage/service/staticLibraryWithC/api10/src/main/ets'))
      .add(copyTmp('/module/stage/service/staticLibrary/api10/src/main/resources'))
      .add(renderTmp('/module/stage/service/staticLibrary/api10/src/main/module.json5'))
    )
    .add(copyTmp('/module/stage/service/common/api10/src/test'))
  )
  .add(renderTmp('/module/stage/service/staticLibrary/api10/build-profile.json5'))
  .add(copyTmp('/module/stage/service/common/api10/consumer-rules.txt'))
  .add(copyTmp('/module/stage/service/staticLibrary/api10/hvigorfile.ts'))
  .add(copyTmp('/module/stage/service/staticLibrary/api10/Index.ets'))
  .add(copyTmp('/module/stage/service/common/api10/obfuscation-rules.txt'))
  .add(renderTmp('/module/stage/service/staticLibrary/api10/oh-package.json5'));

moduleBuilder.add(staticLibraryWithCOption, staticLibraryWithCTemplate, libraryWithCInitFun);
